package org.glow.changgou.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.glow.changgou.goods.dao.AlbumMapper;
import org.glow.changgou.goods.pojo.Album;
import org.glow.changgou.goods.service.AlbumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @Author Glow
 * @Date 2022-09-22 13:00:16
 * @Description
 * @Version 1.0
 */
@Service
public class AlbumServiceImpl extends ServiceImpl<AlbumMapper, Album> implements AlbumService {
    @Autowired
    private AlbumService albumService;
    @Override
    public Page<Album> findPage(Album album, int page, int size) {
        Page<Album> pageInfo = new Page<>(page,size);
        LambdaQueryWrapper<Album> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = getCondition(queryWrapper,album);
        return  albumService.page(pageInfo,queryWrapper);

    }

    @Override
    public Page<Album> findPage(int page, int size) {
        Page<Album> pageInfo = new Page<>(page,size);
        LambdaQueryWrapper<Album> queryWrapper = new LambdaQueryWrapper<>();
        return albumService.page(pageInfo);
    }

    @Override
    public List<Album> findList(Album album) {
        LambdaQueryWrapper<Album> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = getCondition(queryWrapper,album);
        return albumService.list(queryWrapper);
    }

    @Override
    public void delete(Long id) {
        albumService.removeById(id);
    }

    @Override
    public void update(Album album) {
        LambdaQueryWrapper<Album> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Album::getId,album.getId());
        albumService.update(album,queryWrapper);
    }

    @Override
    public void add(Album album) {
            albumService.save(album);
    }

    @Override
    public Album findById(Long id) {
        return albumService.getById(id);
    }

    @Override
    public List<Album> findAll() {

        return albumService.list();
    }


    public LambdaQueryWrapper<Album> getCondition(LambdaQueryWrapper<Album> queryWrapper,Album album)
    {
        if(album!=null){
            // 编号
            if(!StringUtils.isEmpty(album.getId())){
                queryWrapper.eq(Album::getId,album.getId());
            }
            // 相册名称
            if(!StringUtils.isEmpty(album.getTitle())){
                queryWrapper.like(Album::getTitle,album.getTitle());
            }
            // 相册封面
            if(!StringUtils.isEmpty(album.getImage())){
                queryWrapper.eq(Album::getImage,album.getImage());
            }
            // 图片列表
            if(!StringUtils.isEmpty(album.getImageItems())){
                queryWrapper.eq(Album::getImageItems,album.getImageItems());
            }

        }
        return queryWrapper;
    }

}
